#%%
import numpy as np

# %%
file_name = "normal.txt"
dat_str = np.loadtxt(file_name, dtype=np.str, delimiter=' ', unpack=False)
print(dat_str.shape)
print(dat_str.size)
print(dat_str[:5])
print(dat_str[:5, :11])
# %%
dat_str = dat_str[:, :11]
dat_str = dat_str.reshape(dat_str.size)
print(dat_str.shape)
print(dat_str[:20])
# %%
data_car_int = []
for data_hex in dat_str:
    data_int = '0x' + data_hex
    data_car_int.append(int(data_int, 16))
data_car_int = np.array(data_car_int)
# data_car_int = np.expand_dims(data_car_int, 0)
# print(data_car_int.shape)
data_car_int = np.reshape(data_car_int, (-1, 11))
# print(data_car_int.shape)
# print(type(data_car_int))
print(data_car_int)
# %%
list_car = []
for data in data_car_int:
    if data[10] != 90:
        print(data)
        raise ValueError('Error Data!')
        break
    time = (data[0]<<8) + data[1]
    # print(time)
    l, ml, m, mr, r, fl, fr, pwm = data[2], data[3], data[4], data[5], \
                                       data[6], data[7], data[8], data[9]
    list_car.append([l,ml,m,mr,r,fl,fr,pwm])
print(list_car[:5])
# %%
list_car = np.array(list_car, dtype="int8")
x_ad_data = list_car[:, :-1]
y_pwm_data = list_car[:, -1]
print(x_ad_data[:5])
print(y_pwm_data[:5])
# %%
import numpy as np
def LoadData():
    """加载训练集和测试集"""
    x_train = np.load('./ad_train_data.npy')
    x_test = np.load('./ad_test_data.npy')
    y_train = np.load('./pwm_train_label.npy')
    y_test = np.load('./pwm_test_label.npy')
    return x_train, x_test, y_train, y_test
# %%
ad_size = 10
x_train, x_test, y_train, y_test = LoadData()
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
x_train = x_train.reshape(int(x_train.size /ad_size/7), ad_size,7,1)
x_test = x_test.reshape(int(x_test.size /ad_size/7), ad_size,7,1)
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
#print(x_train[:5])
# %%
print(x_train.flatten().shape)
# %%
x_train = x_train.astype('int8')
x_test = x_test.astype('int8')
y_train = y_train.astype('int8')
y_test = y_test.astype('int8')
x_train = (x_train / 128).astype('float32')
x_test = (x_test / 128).astype('float32')
y_train = (y_train / 128).astype('float32')
y_test = (y_test / 128).astype('float32')
print('x_test data shape:%f~%f' %(min(x_train.flatten()),max(x_train.flatten())))
print('y_test data shape:%f~%f' %(min(y_train),max(y_train)))
# %%
